import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

public class Permutations {


    List<List<Integer>> ans = new ArrayList<List<Integer>>();
    List<Integer> path = new ArrayList<Integer>();
    public List<List<Integer>> permute(int[] nums) {
        boolean[] used = new boolean[nums.length];
        backTrending(nums,used,0);
        return ans;
    }

    public void backTrending(int[] nums,boolean[] used,int count){
        if (count==nums.length){
            ans.add(new ArrayList<>(path));
            return;
        }
        for (int i=0;i<used.length;i++){
            if (!used[i]){
                path.add(nums[i]);
                used[i]=true;
                backTrending(nums,used,count+1);
                path.remove(path.size()-1);
                used[i]=false;
            }
        }



    }

}
